package com.example.administrator.xinyutg.task;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2015/9/27.
 */
public class ImageTask extends AsyncTask<String,Void,Bitmap> {

    private ImageView iv;

    public ImageTask(ImageView iv) {
        this.iv = iv;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap=null;
        try {
            URL url=new URL(params[0]);
            try {
                HttpURLConnection con= (HttpURLConnection) url.openConnection();
                if (con.getResponseCode()==200){
                    InputStream is=con.getInputStream();
                    return bitmap= BitmapFactory.decodeStream(is);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        iv.setImageBitmap(bitmap);
    }
}
